package org.example.practiceexamples.mapper;

import lombok.extern.slf4j.Slf4j;
import org.example.practiceexamples.dox.Appointment;
import org.example.practiceexamples.dox.Course;
import org.example.practiceexamples.dto.CourseAppointment;
import org.springframework.jdbc.core.ResultSetExtractor;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class CourseAppointmentResultSetExtractor implements ResultSetExtractor<CourseAppointment> {

    @Override
    public CourseAppointment extractData(ResultSet rs) throws SQLException {
        List<Appointment> appointments = new ArrayList<>();
        Course course = null;
//        CourseAppointment.CourseAppointmentBuilder builder = CourseAppointment.builder();
        while(rs.next()) {
            if(course == null) {
                course = Course.builder()
                        .id(rs.getString("c.id"))
                        .name(rs.getString("name"))
                        .lesson(rs.getInt("lesson"))
                        .credit(rs.getFloat("credit"))
                        .teacherId(rs.getString("teacher_id"))
                        .quantity(rs.getInt("quantity"))
                        .classNumber(rs.getString("class_number"))
                        .theoreticalHours(rs.getInt("theoretical_hours"))
                        .experimentalHours(rs.getInt("experimental_hours"))
                        .build();
            }
            Appointment a = Appointment.builder()
                    .id(rs.getString("a.id"))
                    .course(rs.getString("course"))
                    .teacher(rs.getString("teacher"))
                    .labId(rs.getString("lab_id"))
                    .week(rs.getInt("week"))
                    .labName(rs.getString("lab_name"))
                    .dayofweek(rs.getInt("dayofweek"))
                    .section(rs.getInt("section"))
                    .build();
            appointments.add(a);
        }
        return CourseAppointment.builder()
                .course(course)
                .appointments(appointments)
                .build();
    }

}
